package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Registration extends HttpServlet {

	private static final long serialVersionUID = 6262164576019459505L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Registration() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head>");
		out.print("<title>Successful registration</title>");
		out.print("</head>");
		out.print("<body>");
		out.print("<h1>Successful registration</h1>");
		out.print("<h3>Your account informations:</h3>");
		out.print("<span>Firstname: "+request.getParameter(getFullParamName(request, "fname"))+"</span><br/>");
		out.print("<span>Lastname: "+request.getParameter(getFullParamName(request, "lname"))+"</span><br/>");
		out.print("<span>Username: "+request.getParameter(getFullParamName(request, "username"))+"</span><br/>");
		out.print("<a href=\""+request.getContextPath()+"\">Back</a>");
		out.print("</body>");
		out.print("</html>");
	}
	
	@SuppressWarnings("rawtypes")
	private String getFullParamName(HttpServletRequest request, String name) {
		Enumeration  e = request.getParameterNames();
		while(e.hasMoreElements()){
			String pName = (String)e.nextElement();
			if (pName.endsWith(name)) {
				return pName;
			}
		}
		return null;
	}

}
